package com.onpositive.datacollection.editor;

import java.util.ArrayList;
import java.util.Arrays;

import com.onpositive.datamap.core.IDataProperty;
import com.onpositive.datamap.core.IPropertyGroup;
import com.onpositive.datamap.core.impl.DataCollection;
import com.onpositive.datamap.core.impl.DataProperty;
import com.onpositive.semantic.model.api.changes.HashDelta;
import com.onpositive.semantic.model.api.changes.ObjectChangeManager;
import com.onpositive.semantic.model.api.command.ICommand;
import com.onpositive.semantic.model.api.command.SimpleOneArgCommand;
import com.onpositive.semantic.model.api.order.IOrderMaintainer;
import com.onpositive.semantic.model.api.property.DynamicProperty;
import com.onpositive.semantic.model.api.property.PropertyComparator;
import com.onpositive.semantic.model.api.property.java.JavaPropertyProvider;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.api.realm.OrderedRealm;
import com.onpositive.semantic.model.api.realm.Realm;
import com.onpositive.semantic.model.api.realm.SortingRealm;


public class PropertyGroups extends CollectionView {

	ArrayList<String>groups=new ArrayList<String>();
	private SortingRealm q;
	public PropertyGroups() {
		super("groups.dlf");
		groups.add("1");
		groups.add("2");
		groups.add("3");
	}
	
	public IRealm<IPropertyGroup> getData(){
		DataCollection dataCollection = (DataCollection)DataCollectionAcess.collection();
		if (dataCollection==null){
			return null;
		}
		return dataCollection.getPropertyGroups();		
	}

	@Override
	protected void doRefresh() {
		getBinding("Data").refresh();
	}

	public SortingRealm getProps(){
		if (q!=null){
			return q;
		}
		DataCollection dataCollection = (DataCollection)DataCollectionAcess.collection();
		if (dataCollection==null){
			return null;
		}
		ArrayList<IDataProperty> arrayList = (ArrayList<IDataProperty>)new ArrayList<IDataProperty>( Arrays.asList(dataCollection.getProperties()));
		OrderedRealm<IDataProperty>p=new OrderedRealm<IDataProperty>(arrayList){
			@Override
			protected void executeSimpleCommand(ICommand cmd,
					HashDelta<Object> dlt, SimpleOneArgCommand acmd,
					Object target) {
				DataProperty q=(DataProperty) target;
				if (acmd.getKind().equals(ICommand.UP_VALUE)){
					q.setPriority(q.getPriority()-1);
					dlt.markChanged(target);
					dlt.setOrderChanged(true);
					ObjectChangeManager.markChanged(q);
				}
				if (acmd.getKind().equals(ICommand.DOWN_VALUE)){
					q.setPriority(q.getPriority()+1);
					dlt.markChanged(target);
					dlt.setOrderChanged(true);
					ObjectChangeManager.markChanged(q);
				}
				
			}
		};
		q = new SortingRealm(p, new PropertyComparator(new DynamicProperty("Priority")));
		q.registerService(IOrderMaintainer.class,new IOrderMaintainer() {
			
			@Override
			public int compare(Object arg0, Object arg1) {
				return 0;
			}
			
			@Override
			public boolean canMove(Object obj, boolean direction) {
				return true;
			}
		});
		return q;		
	}
}
